package com.hoshino.momochi.controller;

import com.hoshino.momochi.annotation.IgnoreLogin;
import com.hoshino.momochi.config.Trie.Trie;
import com.hoshino.momochi.model.DTO.BlogDTO;
import com.hoshino.momochi.model.DTO.CommentDTO;
import com.hoshino.momochi.model.domain.Blog;
import com.hoshino.momochi.model.domain.Comment;
import com.hoshino.momochi.service.CommentService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import com.hoshino.momochi.util.PageCheck;
import com.hoshino.momochi.util.SomeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private Trie trie;

    private final String USER_ID = CodeEnum.USERID.getMessage();

    /**
     * 添加评论
     *
     * @param request
     * @param commentDTO 需要字段 content,blog_id
     * @return
     */
    @RequestMapping("api/comment/add")
    public JsonResponse addComment(HttpServletRequest request, @RequestBody CommentDTO commentDTO) {
        Comment comment = new Comment();
        String content = commentDTO.getContent();

        if (!StringUtils.isNotBlank(content)) {
            return JsonResponse.success(CodeEnum.NOTNULL.getCode(), CodeEnum.NOTNULL.getMessage());
        }

        content = trie.synchronizeSensitiveWordReplace(content); // 敏感词替换

        comment.setContent(content);

        comment.setCreateTime(new Date());
        Long uid = (Long) request.getAttribute(USER_ID);

        comment.setUserId(uid);

        Long blogId = commentDTO.getBlog_id();

        if (blogId == null || blogId <= 0) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }

        comment.setBlogId(blogId);

        commentService.addComment(comment);

        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 删除评论
     *
     * @param request
     * @param commentDTO 需求字段 id
     * @return
     */
    @RequestMapping("api/comment/del")
    public JsonResponse delCommentByTwoId(HttpServletRequest request, @RequestBody CommentDTO commentDTO) {
        Long uid = (Long) request.getAttribute(USER_ID); // 用户id
        Long id = commentDTO.getId();//评论 id
        if (Objects.nonNull(id) && id > 0) {
            commentService.delCommentByTwoId(id, uid);
        }
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取指定用户的 所有评论 + 博客信息
     *
     * @param request
     * @return
     */
    @RequestMapping("api/comment/get")
    public JsonResponse getUserComment(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Comment> commentList = commentService.getUserComment(uid);
        return JsonResponse.success(PageCheck.makeData(SomeUtils.sortByCreateTime(commentList)), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取 我的评论的博客信息列表 Blog 不包含评论信息
     *
     * @param request 分页 need_page page_size(null)
     * @return
     */
    @RequestMapping("api/comment/getReplyBlogInfo")
    public JsonResponse getUserReplyBlogInfo(HttpServletRequest request, @RequestBody BlogDTO blogDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Blog> bl = commentService.getMyReplyBlogInfo(uid);
        return JsonResponse.success(PageCheck.pageCheck(SomeUtils.sortByCommentCreateTime(bl), blogDTO.getNeed_page(), blogDTO.getPage_size()), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取指定博客的评论
     *
     * @param request
     * @param commentDTO
     * @return
     */
    @RequestMapping("api/comment/blog")
    @IgnoreLogin
    public JsonResponse getTreeComment(HttpServletRequest request, @RequestBody CommentDTO commentDTO) {
        Long bid = commentDTO.getBlog_id();
        List<Comment> list = commentService.getTreeComment(bid);
        return JsonResponse.success(SomeUtils.sortByCreateTime(list), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 评论回复
     *
     * @param request
     * @param commentDTO 需求字段 blog_id content parent_id(根评论id) reply_id(回复评论id)
     * @return
     */
    @RequestMapping("api/comment/reply")
    public JsonResponse replyComment(HttpServletRequest request, @RequestBody CommentDTO commentDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        // 校验是否正确输入
        if (!StringUtils.isNotBlank(commentDTO.getContent()) || Objects.isNull(commentDTO.getParent_id()) || Objects.isNull(commentDTO.getBlog_id()) || Objects.isNull(commentDTO.getReply_id())) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }

        Comment comment = new Comment();
        Date createTime = new Date();
        commentDTO.setUser_id(uid);
        commentDTO.setCreate_time(createTime);
        comment.setCreateTime(createTime);
        comment.setContent(commentDTO.getContent());
        comment.setReplyId(commentDTO.getReply_id());
        comment.setUserId(uid);
        comment.setParentId(commentDTO.getParent_id());
        comment.setBlogId(commentDTO.getBlog_id());

        commentService.replyComment(comment);
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取谁回复了我 列表
     *
     * @param request
     * @return
     */
    @RequestMapping("api/comment/getReply")
    public JsonResponse getReplyCommentFromMe(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Comment> cl = commentService.getReplyCommentFromMe(uid);
        return JsonResponse.success(SomeUtils.sortByCreateTime(cl), CodeEnum.SUCCESS.getMessage());
    }
}
